package cz.uvn.orthopedy.server.jdo;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import cz.uvn.orthopedy.client.entity.JournalEntry;
import cz.uvn.orthopedy.client.entity.Operation;

public final class JournalJdo {
	
	private JournalJdo() {
	}

	public static void editEntry(JournalEntry entry) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(JournalEntry.class);
			query.setFilter("id == idParam");
			query.declareParameters("Long idParam");
			query.setUnique(true);
			JournalEntry result = (JournalEntry) query.execute(entry.getId());
			if (result != null) {
				result.setHallID(entry.getHallID());
				result.setDate(entry.getDate());
				result.setDoctorID(entry.getDoctorID());
				result.setPatientID(entry.getPatientID());
				
				Operation operation = new Operation();
				operation.setType(entry.getOperation().getType());
				operation.setSymbol(entry.getOperation().getSymbol());
				operation.setDiagnosisCode(entry.getOperation().getDiagnosisCode());
				operation.setDiagnosis(entry.getOperation().getDiagnosis());
				operation.setImplant(entry.getOperation().getImplant());
				operation.setTransfusion(entry.getOperation().getTransfusion());
				operation.setAnestesia(entry.getOperation().getAnestesia());
				operation.setAtb(entry.getOperation().isAtb());
				operation.setRtg(entry.getOperation().isRtg());
				operation.setTurnistle(entry.getOperation().isTurnistle());
				operation.setDiabetes(entry.getOperation().isDiabetes());
				operation.setSeptic(entry.getOperation().isSeptic());
				operation.setPozHbsAg(entry.getOperation().isPozHbsAg());
				operation.setInternalComplications(entry.getOperation().getInternalComplications());
				result.setOperation(operation);
			}
		} finally {
			pm.close();
		}
	}

	public static void createEntry(JournalEntry entry) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(entry);
		} finally {
			pm.close();
		}
	}
	
	public static void deleteEntry(JournalEntry entry) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(JournalEntry.class);
			query.setFilter("id == idParam");
			query.declareParameters("Long idParam");
//			query.setUnique(true);
			@SuppressWarnings("unchecked")
			List<JournalEntry> result = (List<JournalEntry>) query.execute(entry.getId());
			assert result.size() == 1;
			pm.deletePersistent(result.get(0));
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings({"unchecked", "deprecation"})
	public static JournalEntry[] getEntries(Date date) {
		Date d1 = new Date(date.getYear(), date.getMonth(), date.getDay(), 0, 0, 0);
		Date d2 = new Date(date.getYear(), date.getMonth(), date.getDay(), 23, 59, 59);
		
		JournalEntry[] entries = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.setDetachAllOnCommit(true);
		try {
			Query q = pm.newQuery(JournalEntry.class);
			q.setOrdering("date");
			q.setFilter("date >= d1Param && date <= d2Param");
			q.declareParameters("java.util.Date d1Param, java.util.Date d2Param");
			List<JournalEntry> result = (List<JournalEntry>) q.execute(d1, d2);
			entries = new JournalEntry[result.size()];
			for (int i = 0; i < result.size(); i++) {
				JournalEntry e = result.get(i);
				Operation o = e.getOperation();
				Operation operation = new Operation(o.getId(), o.getType(), o.getSymbol(),
						o.getDiagnosisCode(), o.getDiagnosis(), o.getImplant(), o.getTransfusion(),
						o.getAnestesia(), o.isAtb(), o.isRtg(), o.isTurnistle(), o.isDiabetes(),
						o.isSeptic(), o.isPozHbsAg(), o.getInternalComplications());
				entries[i] = new JournalEntry(e.getId(), e.getHallID(), e.getDate(),
						e.getDoctorID(), e.getPatientID(), operation);
			}
		} finally {
			pm.close();
		}
		return entries;
	}

}
